package com.bjwl.controller.safty;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.jboss.logging.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.bjwl.base.Constants;
import com.bjwl.base.CurrUser;
import com.bjwl.base.Result;
import com.bjwl.exception.LoginException;
import com.bjwl.exception.SysException;
import com.bjwl.service.safty.LoginService;
import com.bjwl.view.UserView;

@RestController
@RequestMapping("/safty/login")
public class LoginController {
	
	@Autowired
	private LoginService userService;
	@PostMapping("/userLogin")  //GetMapping不支持@RequestBody
	public Result login(@RequestBody UserView userView,HttpSession session) {
		//实现登录，前端传入信息
		try {
			CurrUser currUser = userService.login(userView);
			//将用户存入session中,//使用常量
			session.setAttribute(Constants.SESSION_ATTR_CURRUSER, currUser);
			//可以输出来
			System.out.println(currUser.toString());
			System.out.println("登录sessionid："+session.getId());
			System.out.println("SESSION_ATTR_CURRUSER"+session.getAttribute(Constants.SESSION_ATTR_CURRUSER));
			return Result.success("登陆成功!");
		} catch (SysException e) {
			return Result.fail("系统升级中");
		}catch(LoginException e) {
			return Result.fail(e.getMessage());
		}
		
	}
	//返回未登录标记
	@GetMapping("/isLogin")
	public Result isLogin() {
		return Result.unLogin();
	}

}
